package com.bdqn.web;

import com.bdqn.dao.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Axin
 * @date 2018/11/9 15:08
 */
public class BaseController {

//    @Resource
//    public SySUserService sySUserService;

//    @Resource
//    private MySecurityMetadataSource mySecurityMetadataSource;

    @Autowired
    protected HttpServletRequest request;

    @Autowired
    protected HttpServletResponse response;

    public ResultVo renderRb(Object data) {
        return this.renderRb(data, "成功");
    }

    public ResultVo renderRb(Object data, String retmsg) {
        ResultVo resultVo = new ResultVo();
        resultVo.setCode(0);
        resultVo.setMsg(retmsg);
        resultVo.setData(data);
        return resultVo;
    }

//    public PaginationResultVo renderRbList(List list) {
//        return this.renderRbList(list, "成功");
//    }
//
//    public PaginationResultVo renderRbList(List list, String retmsg) {
//        if (list instanceof Page) {
//            Page page = (Page) list;
//            PaginationResultVo resultVo = new PaginationResultVo(page);
//            resultVo.setRetmsg(retmsg);
//            resultVo.setData(list);
//            return resultVo;
//        } else {
//            PaginationResultVo resultVo = new PaginationResultVo();
//            resultVo.setRetmsg(retmsg);
//            resultVo.setData(list);
//            return resultVo;
//        }
//    }

    public ResultVo renderSucc() {
        ResultVo resultVo = new ResultVo();
        return resultVo;
    }

    public ResultVo renderError(Integer code, String retmsg) {
        ResultVo resultVo = new ResultVo();
        resultVo.setCode(code);
        resultVo.setMsg(retmsg);
        return resultVo;
    }

    public ResultVo renderError(String retmsg) {
        ResultVo resultVo = new ResultVo();
        resultVo.setCode(-1);
        resultVo.setMsg(retmsg);
        return resultVo;
    }

    /**
     * 获取当前登录的后台管理员
     *
     * @return
     */
//    public SySUser getSySUser() {
//        UserDetails user = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        SySUser u = sySUserService.findByUsername(user.getUsername());
//        return u;
//    }

    /**
     * 获取当前登录的后台管理员
     *
     * @return`
     */
//    public SySUser getSySUserByToken(String token) {
//        Claims claims = Jwts.parser()
//                .setSigningKey(SecurityConstant.JWT_SIGN_KEY)
//                .parseClaimsJws(token)
//                .getBody();
//        //获取用户名
//        String username = claims.getSubject();
//        SySUser u = sySUserService.findByUsername(username);
//        return u;
//    }
//
//    public Void resetPermission() {
//        //重新加载权限
//        mySecurityMetadataSource.loadResourceDefine();
//    }
}
